// BaseBbsState.cpp: implementation of the CBaseBbsState class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansiterminal.h"
#include "BaseBbsState.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBaseBbsState::CBaseBbsState()
{
	m_pKeyboard = NULL;
}

CBaseBbsState::~CBaseBbsState()
{

}

BOOL CBaseBbsState::CheckState(CAnsiScreen *pScreen, CPoint ptCursorCell, IBbsState *pPrevState)
{
	m_pScreen = pScreen;
	return FALSE;
}

void CBaseBbsState::Activate()
{
	// do nothing
}

void CBaseBbsState::Deactivate()
{
	// do nothing
}

HCURSOR CBaseBbsState::GetCursor(CPoint ptCursorCell)
{
	return NULL;
}

BOOL CBaseBbsState::OnClick(CPoint ptCursorCell)
{
	// do nothing
	return FALSE;
}

int CBaseBbsState::colCharCount(char** chars, int x, int minY, int maxY)
{
	int charCount = 0;
	for(int y = minY; y <= maxY; y++)
	{
		if(chars[y][x] != 32)
		{
			++charCount;
			if(charCount > 2) // optimized
			{
				break;
			}
		}
	}
	return charCount;
}

int CBaseBbsState::lastNonSpaceCol(char** chars, int y, int minX, int maxX)
{
	for(int x = maxX; x >= minX; --x)
	{
		if(chars[y][x] != 32)
		{
			return x;
		}
	}
	return -1;
}

void CBaseBbsState::SetKeyboard(IVirtualKeyboard *pKeyboard)
{
	m_pKeyboard = pKeyboard;
}

BOOL CBaseBbsState::OnMouseDown(CPoint ptCell, UINT nFlags)
{
	//OnMouseMove(ptCell, nFlags);
	return FALSE;
}

BOOL CBaseBbsState::OnMouseMove(CPoint ptCell, UINT nFlags)
{
	HCURSOR hCursor = GetCursor(ptCell);
	m_pScreen->SetCursor(hCursor);
	return BOOL(hCursor);
}

BOOL CBaseBbsState::OnMouseUp(CPoint ptCell, UINT nFlags)
{
	return OnClick(ptCell);
}


void CBaseBbsState::setScreenEffects(RECT* pRect, BYTE effect)
{
	for(int y = pRect->top; y <= pRect->bottom; y++)
	{
		for(int x = pRect->left; x <= pRect->right; x++)
		{
			m_pScreen->SetEffect(x, y, effect);
		}
	}
}

BOOL CBaseBbsState::ptOnRect(RECT* pRect, POINT* pPoint)
{
	return  pPoint->x >= pRect->left && pPoint->x <= pRect->right &&
			pPoint->y >= pRect->top && pPoint->y <= pRect->bottom;
}

BOOL CBaseBbsState::OnMouseWheel(CPoint ptCell, UINT nFlags, short wheelNotches)
{
	if(wheelNotches > 0)
	{
		m_pKeyboard->OnKeyDown(VK_UP, 0);
	}
	else
	{
		m_pKeyboard->OnKeyDown(VK_DOWN, 0);
	}
	return TRUE;
}

BOOL CBaseBbsState::OnFreezeMouse(BOOL bFreeze)
{
	return TRUE;
}